package com.hengpeng.msls.service.activity.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfinbase.en.RewardRebateStatus;
import com.hengpeng.itfinbase.persist.RewardRebate;
import com.hengpeng.msls.service.BaseService;
import com.hengpeng.msls.service.activity.RebateService;

@Lazy
@Service
public class RebateServiceImpl extends BaseService implements RebateService {

    @Override
    public ServiceResult<List<RewardRebate>> queryMyList(Long userId, Integer curPage, Integer pageSize) {
        DynamicQuery query = new DynamicQuery(RewardRebate.class);
        if (userId != null) {
            query.eq("userId", userId);
        }
        query.eq("status",RewardRebateStatus.PAID);
        query.desc("createTime");
        query.setPageIndex(curPage);
        query.setPageSize(pageSize);
        query.setIsSearchTotalCount(true);
        query.setQueryType(QueryType.JDBC);
        QueryResult<RewardRebate> result = serviceFactory.getProtFactory().remoteQuery(query, RewardRebate.class);

        Long count = result.getTotalCount();
        ServiceResult<List<RewardRebate>> serviceResult = new ServiceResult<List<RewardRebate>>();// 设置返回值
        serviceResult.setReturnValue(result.getResults());
        serviceResult.setReturnListSize(count.intValue());
        return serviceResult;
    }

    @Override
    public Long countRewardRebate(Long userId, RewardRebateStatus status) {
        Long count = 0L;
        DynamicQuery query = new DynamicQuery(RewardRebate.class);
        query.addProjection(Projections.sum("money"));
        if (userId != null) {
            query.eq("userId", userId);
        }
        if (status != null){
            query.eq("status", status);
        }
        query.setQueryType(QueryType.JDBC);
        System.out.println(query.getSqlResult().getSql());
        List<Object> list = serviceFactory.getProtFactory().findObject(null, query);
        BigDecimal a = (BigDecimal) list.get(0);
        if(a != null){
            count = a.longValue();
        }
        return count;
    }
}
